package com.shiningstars.activity;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.shiningstars.R;
import com.shiningstars.database.DatabaseHandler;
import com.shiningstars.general.Config;
import com.shiningstars.general.Constants;
import com.shiningstars.general.Messages;
import com.shiningstars.general.Utilities;
import com.shiningstars.service.AdminFeedbackService;
import com.shiningstars.service.ErrorService;
import com.shiningstars.service.FeedbackNotifyService;
import com.shiningstars.service.FeedbackSendService;
import com.shiningstars.service.MSLocatorService;
import com.shiningstars.service.ParseService;
import com.shiningstars.service.StatusService;
import com.shiningstars.service.UpdateService;
import com.shiningstars.tools.ExceptionHandler;
import com.shiningstars.valuebeans.Audit;

public class SplashPageActivity extends Activity implements Constants, Messages, Config {

	public static boolean showSplash = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_splash_page);

			Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

			showSplash = false;
			final long startTime = Calendar.getInstance().getTimeInMillis();

			NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			notificationManager.cancelAll();

			new Thread() {
				@Override
				public void run() {

					Intent i = null;
					Looper.prepare();

					if (!Utilities.isRunning(getApplicationContext(), StatusService.class.getName())) {
						startService(new Intent(getApplicationContext(), StatusService.class));
					}

					String status = Utilities.getSharedPreference(getApplicationContext(), STATUS);

					if (!Utilities.isBlankString(status)) {
						final String role = Utilities.getSharedPreference(getApplicationContext(), ROLE);
						String message = null;
						if (ACTIVE.equals(status)) {
							if (ADMIN.equals(role)) {
								i = new Intent(SplashPageActivity.this, AdminActivity.class);

								if (!Utilities.isRunning(getApplicationContext(), AdminFeedbackService.class.getName())) {
									startService(new Intent(getApplicationContext(), AdminFeedbackService.class));
								}
							} else if (TEACHER.equals(role)) {
								i = new Intent(SplashPageActivity.this, MyFeedbackActivity.class);
							}

							String mobileNumber = Utilities.getSharedPreference(getApplicationContext(), MOBILE_NUMBER);
							String imeiNumber = Utilities.getSharedPreference(getApplicationContext(), IMEI_NUMBER);
							String name = Utilities.getSharedPreference(getApplicationContext(), NAME);

							Audit audit = new Audit();
							audit.setType(LOGIN);
							audit.setMobileNumber(mobileNumber);
							audit.setImeiNumber(imeiNumber);
							audit.setDescription(name);
							audit.setVersion(APP_VERSION);
							audit.setDate(Calendar.getInstance().getTimeInMillis());

							DatabaseHandler databaseHandler = DatabaseHandler.getInstance(getApplicationContext());
							long rowId = databaseHandler.logAudit(audit);

							if (rowId == -1) {
								i = null;
								status = ERROR;
								message = SYSTEM_ERROR;
							}

							if (!Utilities.isRunning(getApplicationContext(), FeedbackSendService.class.getName())) {
								startService(new Intent(getApplicationContext(), FeedbackSendService.class));
							}

							if (!Utilities.isRunning(getApplicationContext(), FeedbackNotifyService.class.getName())) {
								startService(new Intent(getApplicationContext(), FeedbackNotifyService.class));
							}

							if (!Utilities.isRunning(getApplicationContext(), MSLocatorService.class.getName())) {
								startService(new Intent(getApplicationContext(), MSLocatorService.class));
							}

							if (!Utilities.isRunning(getApplicationContext(), ParseService.class.getName())) {
								startService(new Intent(getApplicationContext(), ParseService.class));
							}

							if (!Utilities.isRunning(getApplicationContext(), UpdateService.class.getName())) {
								startService(new Intent(getApplicationContext(), UpdateService.class));
							}

							if (!Utilities.isRunning(getApplicationContext(), ErrorService.class.getName())) {
								startService(new Intent(getApplicationContext(), ErrorService.class));
							}

						} else if (INACTIVE.equals(status)) {
							message = REGISTRATION_INACTIVE;
						} else if (PENDING.equals(status)) {
							message = REGISTRATION_PENDING;
						}

						if (!ACTIVE.equals(status)) {
							AlertDialog.Builder builder = new AlertDialog.Builder(SplashPageActivity.this);
							builder.setMessage(message);
							builder.setCancelable(false);
							builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									finish();
								}
							});

							AlertDialog alert = builder.create();
							alert.show();
						}
					} else {
						i = new Intent(SplashPageActivity.this, RegistrationActivity.class);
					}

					if (null != i) {
						final Intent intent = i;
						long endTime = Calendar.getInstance().getTimeInMillis();
						long sleepTime = SPLASH_TIME_OUT - (startTime - endTime);

						Handler handler = new Handler();
						handler.postDelayed(new Runnable() {
							public void run() {
								startActivity(intent);
								finish();
							}
						}, sleepTime);
					}

					Looper.loop();
					Looper.myLooper().quit();
				}
			}.start();
		} catch (Exception e) {
			Utilities.logError(getApplicationContext(), SplashPageActivity.class.getName(), e);
		}
	}
}